﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.MTHDLL;
using thinger.MTHModels;

namespace thinger.MTHProject
{
    public partial class FrmUserManage : Form
    {
        public FrmUserManage()
        {
            InitializeComponent();
            //自动创建列变为false
            this.dgvUser.AutoGenerateColumns = false;

            this.Load += FrmUserManage_Load;
        }

        

        private List<SysAdmin> sysAdminList = new List<SysAdmin>();

        private SysAdminManage sysAdminManage = new SysAdminManage();


        private void FrmUserManage_Load(object sender, EventArgs e)
        {
            List<SysAdmin> sysAdmins = sysAdminManage.AdminLogAll();
            sysAdminList = sysAdmins;

            RefreshDataView(sysAdminList);


        }

        /// <summary>
        /// 清除信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.btnClear.Tag = 0;
            this.txtConfirmPwd.Clear();
            this.txtLoginName.Clear();
            this.txtLoginPwd.Clear();

            ToCheck(false);
            this.btnCheck.Tag = 0;

        }

        private void ToCheck(bool checkBox) 
        {
            this.cbAlarm.Checked = checkBox;
            this.cbHistory.Checked = checkBox;
            this.cbRecipe.Checked = checkBox;
            this.cbParamSet.Checked = checkBox;
            this.cbUserManage.Checked = checkBox;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //校验
            if (this.txtLoginName.Text == null || this.txtLoginName.Text.ToString().Length == 0)
            {
                new FrmMsgBoxWithOutAck("用户名字不能空", "请重新设置用户名字").ShowDialog();
            }
            if (sysAdminList != null && sysAdminList.Count != 0 && sysAdminList.Find(c => c.LoginName == this.txtLoginName.Text.ToString().Trim()) != null)
            {
                new FrmMsgBoxWithOutAck("用户名字重复", "请重新设置用户名字").ShowDialog();
            }
            if (this.txtLoginPwd.Text == null || this.txtLoginPwd.Text.ToString().Trim().Length == 0)
            {
                new FrmMsgBoxWithOutAck("密码不能为空", "请设置密码").ShowDialog();
            }
            if (this.txtConfirmPwd.Text == null || this.txtConfirmPwd.Text.ToString().Trim().Length == 0)
            {
                new FrmMsgBoxWithOutAck("确认密码不能为空", "请设置确认密码").ShowDialog();
            }
            if (this.txtLoginPwd.Text.ToString().Trim() != this.txtConfirmPwd.Text.ToString().Trim())
            {
                new FrmMsgBoxWithOutAck("确认密码不同", "确认密码和密码要相同").ShowDialog();
            }

            SysAdmin sysAdmin = new SysAdmin()
            {
                LoginName = this.txtLoginName.Text.ToString().Trim(),
                LoginPwd = this.txtLoginPwd.Text.ToString().Trim(),
                Recipe = this.cbRecipe.Checked,
                ParamSet = this.cbParamSet.Checked,
                HistoryLog = this.cbHistory.Checked,
                HistoryTrend = this.cbAlarm.Checked,
                UserManage = this.cbUserManage.Checked,
            };
            sysAdminList.Add(sysAdmin);

            //更新Mysql
            int v = sysAdminManage.AddLogin(sysAdmin);

            if (v < 0) 
            {
                new FrmMsgBoxWithOutAck("添加用户失败","请检查用户配置").ShowDialog();
            }

            //刷新数据
            RefreshDataView(sysAdminList);



        }
        //刷新数据
        private void RefreshDataView(List<SysAdmin> sysAdminList)
        {
            this.dgvUser.DataSource = null;
            this.dgvUser.DataSource = sysAdminList;
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            
            string checkAll = this.btnCheck.Tag.ToString().Trim();

            if (checkAll == null)
            {
                this.btnCheck.Tag = 1;
                ToCheck(true);
            }
            else
            {
                string v = checkAll.ToString().Trim() == "1" ? "0" : "1";
                if (v == "1")
                {
                    ToCheck(true);
                    this.btnCheck.Tag = 1;
                }
                else
                {
                    ToCheck(false);
                    this.btnCheck.Tag = 0;
                }
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //校验
            if (this.txtLoginName.Text == null || this.txtLoginName.Text.ToString().Length == 0)
            {
                new FrmMsgBoxWithOutAck("用户名字不能空", "请重新设置用户名字").Show();
            }
            if (sysAdminList == null || sysAdminList.Count == 0 || sysAdminList.Find(c => c.LoginName == this.txtLoginName.Text.ToString().Trim()) == null)
            {
                new FrmMsgBoxWithOutAck("不存在该户名", "请先配置用户").Show();
            }
            if (this.txtLoginPwd.Text == null || this.txtLoginPwd.Text.ToString().Trim().Length == 0)
            {
                new FrmMsgBoxWithOutAck("密码不能为空", "请设置密码").Show();
            }
            if (this.txtConfirmPwd.Text == null || this.txtConfirmPwd.Text.ToString().Trim().Length == 0)
            {
                new FrmMsgBoxWithOutAck("确认密码不能为空", "请设置确认密码").Show();
            }
            if (this.txtLoginPwd.Text.ToString().Trim() != this.txtConfirmPwd.Text.ToString().Trim())
            {
                new FrmMsgBoxWithOutAck("确认密码不同", "确认密码和密码要相同").Show();
            }
            SysAdmin sysAdminOld = sysAdminList.Find(c => c.LoginName == this.txtLoginName.Text.ToString().Trim());

            sysAdminOld.LoginName = this.txtLoginName.Text.ToString().Trim();
            sysAdminOld.LoginPwd = this.txtLoginPwd.Text.ToString().Trim();
            sysAdminOld.Recipe = this.cbRecipe.Checked;
            sysAdminOld.ParamSet = this.cbParamSet.Checked;
            sysAdminOld.HistoryLog = this.cbHistory.Checked;
            sysAdminOld.HistoryTrend = this.cbAlarm.Checked;
            sysAdminOld.UserManage = this.cbUserManage.Checked;
            

            //更新Mysql
            int v = sysAdminManage.UpdateLogin(sysAdminOld);

            if (v < 0)
            {
                new FrmMsgBoxWithOutAck("修改用户失败", "请检查用户配置").Show();
            }

            //刷新数据
            RefreshDataView(sysAdminList);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //校验
            if (this.txtLoginName.Text == null || this.txtLoginName.Text.ToString().Length == 0)
            {
                new FrmMsgBoxWithOutAck("用户名字不能空", "请重新设置用户名字").Show();
            }
            if (sysAdminList == null || sysAdminList.Count == 0 || sysAdminList.Find(c => c.LoginName == this.txtLoginName.Text.ToString().Trim()) == null)
            {
                new FrmMsgBoxWithOutAck("不存在该户名", "请先配置用户").Show();
            }
            DialogResult dialogResult = new FrmMsgBoxWithAck("确认删除", "是否删除用户").ShowDialog();

            if (dialogResult == DialogResult.OK) 
            {
                SysAdmin sysAdmin = sysAdminList.Find(c => c.LoginName == this.txtLoginName.Text.ToString().Trim());
                sysAdminList.Remove(sysAdmin);
                sysAdminManage.DeleteLogin(sysAdmin);
                RefreshDataView(sysAdminList);
            }


        }

        //设置点击显示信息
        private void dgvUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
           if (e.RowIndex >= 0) 
           {
               string LoginName = this.dgvUser.Rows[e.RowIndex].Cells[0].Value.ToString().Trim();

               SysAdmin sysAdmin = sysAdminList.Find(c => c.LoginName == LoginName);

               if (sysAdmin != null)
               {
                   this.txtLoginName.Text = sysAdmin.LoginName;
                   this.txtLoginPwd.Text = sysAdmin.LoginPwd;
                   this.txtConfirmPwd.Text = sysAdmin.LoginPwd;
                   this.cbAlarm.Checked = sysAdmin.HistoryTrend;
                   this.cbHistory.Checked = sysAdmin.HistoryLog;
                   this.cbParamSet.Checked = sysAdmin.ParamSet;
                   this.cbRecipe.Checked = sysAdmin.Recipe;
                   this.cbUserManage.Checked = sysAdmin.UserManage;
               }
           }
                

            
        }
        
        
        //将内容的布尔换成字符
        private void dgvUser_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                object value = this.dgvUser.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (value != null) 
                {
                    if (value.ToString().Trim().ToLower() == "true")
                    {
                        this.dgvUser.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "启用";

                    }
                    else
                    {
                        this.dgvUser.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "禁用";
                    }
                }
                
            }
        }
        //设置自动序号
        private void dgvUser_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.dgvUser.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.dgvUser.RowHeadersDefaultCellStyle.Font, rectangle, this.dgvUser.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
    }
}
